package com.example.api.controller;


import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * Create with Intellij idea
 * User:鲁智明
 * Date:2018/11/16
 * Time:23:07
 */
@RestController
public class OrderController {

    /**
     * SpringCloud 二种调用服务接口的方式Rest或者fegin
     *
     */

    @Resource
    private RestTemplate restTemplate;
    //获取注册中心的信息的API
    @Resource
    private DiscoveryClient discoveryClient;

    @RequestMapping("/getOrder")
    public String getOrder(){
        String memberUrl="http://zk-member/getMember";
        return restTemplate.getForObject(memberUrl,String.class);
    }

    //默认rest方式开启负载均衡功能，如果以服务别名zk-member名称进行调用服务接口，必须使用@LoadBalanced主键
    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }

    //获取注册中上的服务相关信息
    @RequestMapping("/getDiscoveryClient")
    public List<ServiceInstance> getDiscoveryClient(){
        List<ServiceInstance> instances = discoveryClient.getInstances("zk-member");
        for (ServiceInstance serviceinstance:instances) {
            System.out.println("utl:"+serviceinstance.getUri());
        }
        return instances;
    }


}
